package com.yzg.redis.mapper;

import java.io.IOException;
import java.net.URI;
import java.util.List;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;



@Service("httpPostManager")
public class HttpPostManager{
	private transient final Logger log = LoggerFactory.getLogger(getClass());

	private PoolingHttpClientConnectionManager cm;

	private CloseableHttpClient cc;


	private RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(30000).build();

	public synchronized PoolingHttpClientConnectionManager getPHCManagerInstance() {
		if (cm == null) {
			cm = new PoolingHttpClientConnectionManager();
			cm.setMaxTotal(200);
			cm.setDefaultMaxPerRoute(200);
		}
		return cm;
	}
	
	public synchronized CloseableHttpClient getCloseableHttpClient() {
		if (cc == null) {
			cc = HttpClients.custom().setConnectionManager(getPHCManagerInstance()).build();
		}
		return cc;
	}

	public String getResponseByPost(String url,List<NameValuePair> formparams, String path) {
		String content = "";
		CloseableHttpClient httpClient = getCloseableHttpClient();
		UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
		try {
			URI uri = new URIBuilder().setScheme("http").setHost(url).setPath(path).build();
			HttpPost httppost = new HttpPost(uri);
			httppost.setEntity(entity);
			httppost.setConfig(requestConfig);
			CloseableHttpResponse response = httpClient.execute(httppost, HttpClientContext.create());
			content = getResponseContent(response);
		} catch (Exception e) {
			log.error("" + e, e);
			e.printStackTrace();
		}
		return content;
	}

	private String getResponseContent(CloseableHttpResponse response) {
		String content = "";
		if (response != null) {
			HttpEntity httpEntity = response.getEntity();
			try {
				if (response.getStatusLine().getStatusCode() == 200) {
					if (httpEntity != null) {
						content = EntityUtils.toString(httpEntity);
					}
				} else {
					content = "" + response.getStatusLine().getStatusCode();
				}
			} catch (Exception e) {
				log.error("http connect error:maybe connect time > 3S       " + e, e);
				return "500";
			} finally {
				try {
					if (httpEntity != null) {
						httpEntity.getContent().close();
					}
					response.close();
				} catch (IOException e) {
				}
			}
		}
		return content;
	}
}
